package com.crlui.component
{
	import com.crlui.component.itemRender.IListItemRender;
	import com.crlui.component.itemRender.ListItemRender;
	import com.crlui.core.ChooseState;
	import com.crlui.core.IDisplayList;
	import com.crlui.core.IFactory;
	import com.crlui.core.ILayoutable;
	import com.crlui.core.IRenderable;
	import com.crlui.event.ResizeEvent;
	import com.crlui.manager.Pool;
	import com.crlui.manager.RenderManager;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class PageList extends Sprite implements IDisplayList ,IRenderable
	{
		protected var pool:Pool;
		
		protected var _layout:ILayoutable;
		
		private var _children:Vector.<ListItemRender>;
		
		public function PageList(factory:IFactory)
		{
			super();
			
			pool=new Pool(factory);
			
			
			_children=new Vector.<ListItemRender>();
		}
		
		public function set layout(value:ILayoutable):void{
			_layout=value;
			
			if(_children.length){
				_layout.layout(_children);
			}
		}
		
		public function displayList(value:*):void
		{
			var len:int=numChildren		
			var item:ListItemRender;
			
			var dataLen:int=value.length;
			
			for(var i:int=0;i<len;i++){
				item=this.getChildAt(i) as ListItemRender;
				
				if(i>dataLen){
					bindEnabled(item,false);
					pool.add(item);
					
					this.removeChildAt(i);
					continue;
				}
				item.data=value[i];
			}
			
			for(;i<dataLen;i++){
				
				item=pool.get() as ListItemRender;
				bindEnabled(item,true);
				
				addChild(item);
				
				item.data=value[i];
				
				
			}
			
			invalidate();
			
			selectedItem=null;
			
		}
		
		
		public function invalidate():void{
			RenderManager.add(this);
		}
		
		public function render():void{
			
			var len:int=this.numChildren;
			_children.length=len;
			for(var i:int=0;i<len;i++){
				_children[i]=getChildAt(i) as ListItemRender;
			}
			
			if(_layout)_layout.layout(_children);
		}
		
		
		
		protected function clear():void{
			var item:ListItemRender;
			while(this.numChildren){
				item=this.removeChildAt(0) as ListItemRender;
				bindEnabled(item,false);
				
				pool.add(item);
			}
			
			_children.length=0;
		}
		
		protected function bindEnabled(item:ListItemRender,enabled:Boolean):void{
			if(enabled){
				item.addEventListener(MouseEvent.CLICK,clickHandler);
				item.addEventListener(ResizeEvent.RESIZE,resizeHandler);
			}else{
				item.removeEventListener(MouseEvent.CLICK,clickHandler);
				item.addEventListener(ResizeEvent.RESIZE,resizeHandler);
			}
		}
		
		protected function resizeHandler(event:ResizeEvent):void{
			this.dispatchEvent(event);
		}
		
		
		protected var _selectedItem:ListItemRender;
		protected function clickHandler(event:MouseEvent):void{
			var old:ListItemRender=selectedItem
			selectedItem=event.currentTarget as ListItemRender;
			
			if(hasEventListener(Event.SELECT) && (_allowRepeat || old !=_selectedItem)){
				this.dispatchEvent(new Event(Event.SELECT));
			}
		}
		
		private var _allowRepeat:Boolean;
		public function set allowRepeat(value:Boolean):void{
			_allowRepeat=value;
		}
		
		public function get selectedItem():ListItemRender{
			return _selectedItem;
		}
		
		public function set selectedItem(value:ListItemRender):void{
			var isChange:Boolean=_selectedItem !=value
			
			if(_selectedItem){
				if(!isChange){
					return;
				}
				
				_selectedItem.chooseState=ChooseState.UNSELECT;
			}
			
			_selectedItem=value;
			if(_selectedItem){
				_selectedItem.chooseState=ChooseState.SELECT;
			}
			
			if(isChange){
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		
		public function set selectedIndex(value:int):void{
			if(value<0){
				selectedItem=null;
				return;
			}
			if(value>numChildren){
				value=numChildren;
			}
			
			selectedItem=this.getChildAt(value) as ListItemRender;
		}
		
		public function get selectedIndex():int{
			if(selectedItem==null){
				return -1;
			}
			return this.getChildIndex(selectedItem);
		}
		
		
		public function dispose():void
		{
			_children=null;
			
			var len:int=this.numChildren;
			var item:ListItemRender;
			for(var i:int=0;i<len;i++){
				item=(getChildAt(i) as ListItemRender);
				bindEnabled(item,false);
				item.dispose();
			}
			
			pool=null;
		}
		
		public function awaken():void
		{
			var len:int=this.numChildren;
			for(var i:int=0;i<len;i++){
				(getChildAt(i) as IListItemRender).awaken();
			}
		}
		
		public function sleep():void
		{
			var len:int=this.numChildren;
			for(var i:int=0;i<len;i++){
				(getChildAt(i) as IListItemRender).sleep();
			}
		}
		
	}
}